package com.zhou.framework.config;

import com.zhou.util.GroovyEngine;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.io.File;

/**
 * @author lang.zhou
 * @date 2022/4/6 17:23
 */
@Configuration
@Slf4j
public class GroovyConfig {
    @Bean
    public Object getGroovy(Environment env){
        String script = "def getCellValue(Object obj,String key,Object dv){\n" +
                "    Object res;\n" +
                "    if(obj == null || (res = obj[key]) == null){\n" +
                "        return dv;\n" +
                "    }\n" +
                "    return res;\n" +
                "\n" +
                "}";
        String path = env.getProperty("groovy.expand.path");
        GroovyEngine.loadExternalScript(script);
        if(StringUtils.isNotBlank(path)){
            File f = new File(path);
            GroovyEngine.loadExpandFile(f);
        }
        log.info("groovy初始化完成");
        return GroovyEngine.getInstance();
    }
}
